<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mermaid 图表</title>
    <script src="mermaid.min.js"></script>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica', 'Arial', sans-serif;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }
        #mermaid-container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin: 20px;
            max-width: 90%;
            max-height: 80vh;
            overflow: auto;
        }
        #mermaid-diagram {
            max-width: 100%;
        }
        #save-button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            color: white;
            background-color: #007AFF;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        #save-button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div id="mermaid-container">
        <div id="mermaid-diagram"></div>
    </div>
    <button id="save-button">保存图片</button>
    <script>
        mermaid.initialize({ startOnLoad: false });
        function renderMermaid(code) {
            const element = document.getElementById('mermaid-diagram');
            element.innerHTML = code;
            mermaid.init(undefined, element);
            window.webkit.messageHandlers.debug.postMessage("Mermaid diagram rendered successfully");
        }
        renderMermaid('MERMAID_CODE_PLACEHOLDER');

        document.getElementById('save-button').addEventListener('click', function() {
            const svgElement = document.querySelector('#mermaid-diagram svg');
            const svgData = new XMLSerializer().serializeToString(svgElement);
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');
            const img = new Image();
            img.onload = function() {
                // 设置更高的分辨率
                const scale = 4; // 增加分辨率的倍数
                canvas.width = img.width * scale;
                canvas.height = img.height * scale;
                ctx.scale(scale, scale);
                ctx.drawImage(img, 0, 0);
                const pngData = canvas.toDataURL('image/png');
                window.webkit.messageHandlers.saveImage.postMessage(pngData);
            };
            img.src = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svgData)));
        });
    </script>
</body>
</html>
